package com.yunhe.policyfulfillment.policyful.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.policyful.domain.Banner;
import com.yunhe.policyfulfillment.policyful.model.BannerQueryCondition;
import com.yunhe.policyfulfillment.policyful.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: BannerController
 * @Description:
 * @author: GongDaCheng
 * @Date: 15:38 2021/7/21
 */
@Api(value = "Banner", description = "Banner", tags = {"Model_Banner"})
@RestController
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    BannerService bannerService;

    @ApiOperation("新增发布Banner")
    @PostMapping(value = "/create")
    public ResponseEntity<ResultObject<Integer>> create(@RequestBody Banner banner) {
        int count = bannerService.create(banner);
        return new ResponseEntity<>(new ResultObject<>(count), HttpStatus.OK);
    }

    @ApiOperation("根据id查看Banner")
    @GetMapping(value = "/getById")
    public ResponseEntity<ResultObject<Banner>> getById(@RequestParam(value = "id", required = true) Long id){
        Banner byId = bannerService.getById(id);
        return new ResponseEntity<>(new ResultObject<>(byId), HttpStatus.OK);
    }

    @ApiOperation("修改Banner")
    @PutMapping(value = "/update")
    public ResponseEntity<ResultObject<Integer>> update(@RequestBody Banner banner){
        int count = bannerService.update(banner);
        return new ResponseEntity<>(new ResultObject<>(count), HttpStatus.OK);
    }

    @ApiOperation("根据条件获取Banner")
    @PostMapping (value = "/getByCondition")
    public ResponseEntity<ResultObject<List<Banner>>> getByCondition(@ModelAttribute BannerQueryCondition bannerQueryCondition) throws ArgumentErrorException {
        PageResult pageResult = bannerService.getByCondition(bannerQueryCondition);
        return new ResponseEntity<>(new ResultObject(pageResult), HttpStatus.OK);
    }

    @ApiOperation("Banner上线/下线操作")
    @PutMapping(value = "/onlineOffline")
    public ResponseEntity<ResultObject> OnlineOffline(@RequestParam(value = "ids", required = true) String ids,
                                      @RequestParam(value = "targetStatus", required = true) String targetStatus){
        bannerService.onlineOffline(ids, targetStatus);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("Banner置顶操作")
    @PutMapping(value = "/top")
    public ResponseEntity<ResultObject> top(@RequestParam(value = "id", required = true) Long id){
        bannerService.top(id);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("删除Banner")
    @DeleteMapping(value = "/deleteById")
    public ResponseEntity<ResultObject> deleteById(@RequestParam(value = "id", required = true) Long id){
        int count = bannerService.deleteById(id);
        return new ResponseEntity<>(new ResultObject<>(count), HttpStatus.OK);
    }

}
